package com.gw.booking.controller.wechat;

import com.alibaba.fastjson.JSONObject;
import com.gw.booking.common.*;
import com.gw.booking.dto.PageDto;
import com.gw.booking.dto.wechat.*;
import com.gw.booking.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

@Api(tags = {"微信-会员管理"})
@Controller
@RequestMapping("/wechat/customer")
public class WCustomerController {

    @Autowired
    private CustomerService customerService;

    @ApiOperation(value = "微信登录（客户）")
    @RequestMapping(value = "/login.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<PageDto<WLoginDtoOut>> login(HttpServletRequest request
            , @ApiParam(value = "微信code", required = true) @RequestParam String code)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, customerService.txLogin(code, request.getRemoteAddr()));
    }

    @ApiOperation(value = "我的资料")
    @RequestMapping(value = "/profile.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<WCustomerDtoOut> profile(HttpServletRequest request)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, customerService.profile(Util.getLoginToken(request)));
    }

    @ApiOperation(value = "修改姓名")
    @RequestMapping(value = "/changeName.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject<WCustomerDtoOut> changeName(HttpServletRequest request
            , @Valid @RequestBody WCustomerNameDto wCustomerNameDto
            , BindingResult errors)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        customerService.txUpdateName(Util.getLoginToken(request), wCustomerNameDto.getName());
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "修改电话")
    @RequestMapping(value = "/changePhone.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject<WCustomerDtoOut> changePhone(HttpServletRequest request
            , @Valid @RequestBody WCustomerPhoneDto wCustomerPhoneDto
            , BindingResult errors)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        customerService.txUpdatePhone(Util.getLoginToken(request), wCustomerPhoneDto.getPhone());
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "获取SessionKey")
    @RequestMapping(value = "/getSessionKey.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject<String> getSessionKey(HttpServletRequest request
            , @Valid @RequestBody GetPhoneDto getPhoneDto
            , BindingResult errors)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        JSONObject jsonObject = WXAppletUserInfo.getSessionKeyOropenidForDiner(getPhoneDto.getCode());
        String sessionKey = jsonObject.get("session_key") == null ? null : jsonObject.get("session_key").toString();
        if (sessionKey == null) {
            throw new ApiException(MessageCode.CODE_WXCODE_ERROR);
        }
        return new ResultObject(MessageCode.CODE_SUCCESS, sessionKey);
    }

    @ApiOperation(value = "获取电话")
    @RequestMapping(value = "/getPhone.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject<String> getPhone(HttpServletRequest request
            , @Valid @RequestBody GetPhoneDto getPhoneDto
            , BindingResult errors)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        return new ResultObject(MessageCode.CODE_SUCCESS, customerService.wxDecrypt(getPhoneDto.getEncrypData(), getPhoneDto.getSessionKey(), getPhoneDto.getIvData()));
    }

}
